﻿angular.module('app')
    .config([
        '$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
            $stateProvider.state({
                name: 'default',
                url: '',
                views: {
                    "": {
                        templateUrl: 'scripts/app/views/Home/main.html'
                    },
                    "headerbar": { templateUrl: 'scripts/app/views/Home/headerbar.html' },
                    "sidebar": { templateUrl: 'scripts/app/views/Home/sidebar.html' }
                },
                controller: 'HomeCtrl',
                authorize: true,
                resolve: {
                    userLoad: ['$q', '$rootScope', '$timeout', function ($q, $rootScope, $timeout) {
                        var defered = $q.defer();
                        defered.resolve();

                        //abort all previous request
                        $rootScope.$broadcast("abortPendingRequest");

                        return defered.promise;
                    }]
                }
            });

            $stateProvider.state({
                name: 'home',
                url: '/',
                views: {
                    "": {
                        templateUrl: 'scripts/app/views/Home/main.html'
                    },
                    "headerbar": { templateUrl: 'scripts/app/views/Home/headerbar.html' },
                    "sidebar": {
                        templateUrl: 'scripts/app/views/Home/sidebar.html'
                    }
                },
                controller: 'HomeCtrl',
                authorize: true,
                resolve: {
                    userLoad: ['$q', '$rootScope', '$timeout', function ($q, $rootScope, $timeout) {
                        var defered = $q.defer();
                        defered.resolve();

                        //abort all previous request
                        $rootScope.$broadcast("abortPendingRequest");

                        return defered.promise;
                    }]
                }
            });

            $stateProvider.state({
                name: 'signin',
                url: '/signin',
                templateUrl: 'scripts/app/views/Login/index.html'
            });

            $stateProvider.state({
                name: 'userprofile',
                url: '/userprofile',
                views: {
                    "": { templateUrl: 'scripts/app/views/User/index.html' },
                    "headerbar": { templateUrl: 'scripts/app/views/Home/headerbar.html' },
                    "sidebar": {
                        templateUrl: 'scripts/app/views/Home/sidebar.html'
                    }
                },
                authorize: true,
                resolve: {
                    userLoad: ['$q', '$rootScope', '$timeout', function ($q, $rootScope, $timeout) {
                        var defered = $q.defer();
                        defered.resolve();

                        //abort all previous request
                        $rootScope.$broadcast("abortPendingRequest");

                        return defered.promise;
                    }]
                }
            });

            $stateProvider.state({
                name: 'userinfo',
                url: '/userinfo/{username}',
                views: {
                    "": { templateUrl: 'scripts/app/views/User/info.html' },
                    "headerbar": { templateUrl: 'scripts/app/views/Home/headerbar.html' },
                    "sidebar": {
                        templateUrl: 'scripts/app/views/Home/sidebar.html'
                    }
                },
                authorize: true,
                resolve: {
                    fade: ['$rootScope', '$q', '$timeout', function ($rootScope, $q, $timeout) {
                        var defered = $q.defer();
                        defered.resolve();
                        return defered.promise;
                    }]
                }
            });

            $stateProvider.state({
                name: 'task',
                url: '/tasks?toid',
                views: {
                    "": {
                        templateUrl: 'scripts/app/views/Task/list.html'
                    },
                    "headerbar": { templateUrl: 'scripts/app/views/Home/headerbar.html' },
                    "sidebar": {
                        templateUrl: 'scripts/app/views/Home/sidebar.html'
                    }
                },
                authorize: true,
                resolve: {
                    fade: ['$rootScope', '$q', '$timeout', function ($rootScope, $q, $timeout) {
                        var defered = $q.defer();
                        var tm;
                        $rootScope.$on("chagingPage", function() {
                            defered.reject();
                            $timeout.cancel(tm);
                        });
                        defered.resolve();

                        //abort all previous request
                        $rootScope.$broadcast("abortPendingRequest");

                        return defered.promise;
                    }]
                }
            });

            $stateProvider.state({
                name: 'dashboard',
                url: '/dashboard',
                views: {
                    "": {
                        templateUrl: 'scripts/app/views/Dashboard/index.html'
                    },
                    "headerbar": { templateUrl: 'scripts/app/views/Home/headerbar.html' },
                    "sidebar": {
                        templateUrl: 'scripts/app/views/Home/sidebar.html'
                    }
                },
                authorize: true,
                resolve: {
                    fade: ['$rootScope', '$q', '$timeout', function ($rootScope, $q, $timeout) {
                        var defered = $q.defer();
                        var tm;
                        $rootScope.$on("chagingPage", function () {
                            defered.reject();
                            $timeout.cancel(tm);
                        });
                        defered.resolve();

                        //abort all previous request
                        $rootScope.$broadcast("abortPendingRequest");

                        return defered.promise;
                    }]
                }
            });

            $stateProvider.state({
                name: 'holidays',
                url: '/holidays',
                views: {
                    "": { templateUrl: 'scripts/app/views/Holiday/index.html' },
                    "headerbar": { templateUrl: 'scripts/app/views/Home/headerbar.html' },
                    "sidebar": {
                        templateUrl: 'scripts/app/views/Home/sidebar.html'
                    }
                },
                authorize: true,
                resolve: {
                    fade: ['$rootScope', '$q', '$timeout', function ($rootScope, $q, $timeout) {
                        var defered = $q.defer();
                        var tm;
                        $rootScope.$on("chagingPage", function () {
                            defered.reject();
                            $timeout.cancel(tm);
                        });
                        defered.resolve();

                        //abort all previous request
                        $rootScope.$broadcast("abortPendingRequest");

                        return defered.promise;
                    }]
                }
            });

            $stateProvider.state({
                name: 'sketch',
                url: '/sketch?workflow',
                views: {
                    "": { templateUrl: 'scripts/app/views/Sketch/index.html' },
                    "headerbar": { templateUrl: 'scripts/app/views/Home/headerbar.html' },
                    "sidebar": {
                        templateUrl: 'scripts/app/views/Home/sidebar.html'
                    }
                },
                authorize: true,
                resolve: {
                    fade: ['$rootScope', '$q', '$timeout', function ($rootScope, $q, $timeout) {
                        var defered = $q.defer();
                        var tm;
                        $rootScope.$on("chagingPage", function () {
                            defered.reject();
                            $timeout.cancel(tm);
                        });
                        defered.resolve();

                        //abort all previous request
                        $rootScope.$broadcast("abortPendingRequest");

                        return defered.promise;
                    }]
                }
            });

            $stateProvider.state({
                name: 'report',
                url: '/report',
                views: {
                    "": { templateUrl: 'scripts/app/views/Report/index.html' },
                    "headerbar": { templateUrl: 'scripts/app/views/Home/headerbar.html' },
                    "sidebar": {
                        templateUrl: 'scripts/app/views/Home/sidebar.html'
                    }
                },
                authorize: true,
                resolve: {
                    fade: ['$rootScope', '$q', '$timeout', function ($rootScope, $q, $timeout) {
                        var defered = $q.defer();
                        var tm;
                        $rootScope.$on("chagingPage", function () {
                            defered.reject();
                            $timeout.cancel(tm);
                        });
                        defered.resolve();

                        //abort all previous request
                        $rootScope.$broadcast("abortPendingRequest");

                        return defered.promise;
                    }]
                }
            });

            $urlRouterProvider.otherwise("/");
        }
    ]);